<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <a-button type="primary" @click="openForm" v-if="hasPermission(['bpm:form:create'])">
          新增
        </a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '修改',
              onClick: openForm.bind(null, record.id),
              ifShow: hasPermission(['bpm:form:update']),
            },
            {
              icon: 'clarity:note-edit-line',
              label: '详情',
              onClick: openDetail.bind(null, record),
              ifShow: hasPermission(['bpm:form:info']),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              ifShow: hasPermission(['bpm:form:delete']),
              popConfirm: {
                title: '是否要删除数据',
                placement: 'left',
                confirm: handleDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Modal
      v-model:visible="visible"
      title="表单详情"
      @ok="handleOk"
      width="800px"
      :bodyStyle="{
        maxHeight: '600px',
        overflowY: 'auto',
        minHeight: '600px',
        paddingTop: '16px',
        paddingRight: '52px',
      }"
    >
      <form-create :option="detailData.option" :rule="detailData.rule" />
      <el-empty description="暂无数据" v-if="!detailData.rule.length" />
    </Modal>
  </div>
</template>
<script lang="ts" setup>
  import { useGo } from '/@/hooks/web/usePage';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { getFormPage, getForm, deleteForm } from '/@/api/backs/workFlow/form';
  import { columns, searchFormSchema } from './components/tableData';
  import { ref } from 'vue';
  import { setConfAndFields2 } from '/@/utils/formCreate';
  import { Modal, message } from 'ant-design-vue';
  import { usePermission } from '/@/hooks/web/usePermission';
  const detailData = ref({
    rule: [],
    option: {},
  });
  const { hasPermission } = usePermission();
  const go = useGo();
  const visible = ref(false);
  const [registerTable, { reload }] = useTable({
    title: '流程表单列表',
    api: getFormPage,
    columns,
    formConfig: { labelWidth: 120, schemas: searchFormSchema },
    useSearchForm: true,
    showTableSetting: true,
    actionColumn: {
      width: 200,
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: 'right',
    },
  });

  function openForm(id) {
    if (typeof id === 'number') {
      go('/workFlowManage/workFlow/modelEdit/' + id);
    } else {
      go('/workFlowManage/workFlow/modelEdit/' + 'noEdit');
    }
  }

  async function openDetail(record: Recordable) {
    // openModal(true, { record });
    visible.value = true;
    const data = await getForm({ id: record.id });
    setConfAndFields2(detailData, data.conf, data.fields);
    console.log(detailData);
  }
  const handleOk = () => {
    visible.value = false;
  };
  async function handleDelete(record: Recordable) {
    await deleteForm({ id: record.id });
    message.success('删除成功');
    reload();
  }
</script>
